﻿using Joy.Erp.BasicArchives.Localization;
using System.Linq;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Localization;

namespace Joy.Erp.Ufida.Permissions
{
    public class ProductionPlatformPermissionDefinitionPovider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(UfidaPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(UfidaPermissions.Menus, L("Permission:Menus"));
            }

            var productionManagementMenu = menus.GetPermissionOrNull(UfidaPermissions.BasicArchivesMenu.ProductionManagement);
            if (productionManagementMenu == null)
            {
                productionManagementMenu = menus.AddPermission(UfidaPermissions.BasicArchivesMenu.ProductionManagement, L("Permission:Menus.BasicArchives.ProductionManagement"));
            }

            var parentMenu = productionManagementMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.ProductionPlatform.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = productionManagementMenu.AddChild(UfidaPermissions.ProductionPlatform.ParentMenu, L("Permission:Menus.BasicArchives.ProductionManagement.Production"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.ProductionPlatform.Menu);
            if (menu == null)
            {
                menu = parentMenu.AddChild(UfidaPermissions.ProductionPlatform.Menu, L("Permission:Menus.BasicArchives.ProductionManagement.Production.ProductionPlatform"));
            }

            var group = context.GetGroupOrNull(UfidaPermissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup(UfidaPermissions.GroupName, L("Permission:BasicArchives"));
            }

            var pProductionPlatformDefault = group.AddPermission(UfidaPermissions.ProductionPlatform.Default, L("Permission:BasicArchives.Production.ProductionPlatform"));
            var pProductionPlatformQuery = pProductionPlatformDefault.AddChild(UfidaPermissions.ProductionPlatform.Query, L("Permission:Query"));
            var pProductionPlatformEdit = pProductionPlatformDefault.AddChild(UfidaPermissions.ProductionPlatform.Edit, L("Permission:Edit"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
}
